package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.iwhalecloud.citybrain.flow.platform.manager.application.service.FlowCandidateService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.HistoryService;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
　　* @description: 流程任务候选人服务实现
　　* @author shenrm
　　* @date 2021/12/6 21:03
　　*/
@Service("flowTaskCandidateService")
public class FlowTaskCandidateServiceImpl implements FlowCandidateService {

    @Autowired
    private HistoryService historyService;

    @Override
    public String getNgrUser(String processInstanceId, String userCode) {
        if(StringUtils.isBlank(processInstanceId)) {
            return null;
        }

        // 根据流程实例ID获取历史处理的任务实例列表
        List<HistoricTaskInstance> historyTasks = historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstanceId).finished().orderByTaskCreateTime().asc().list();

        // 为空时,则直接返回传的userCode,否则返回该流程第一个节点处理人
        if(CollectionUtils.isEmpty(historyTasks) || StringUtils.isBlank(historyTasks.get(0).getAssignee())) {
            return userCode;
        }
        return historyTasks.get(0).getAssignee();
    }
}
